<?php
/**
 * Created by PhpStorm.
 * User: xsp
 * Date: 2018/5/7
 * Time: 13:37
 */

namespace frontend\controllers;


use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\web\Controller;
use common\models\Category;
use Yii;
use common\models\User;
use backend\models\Product;
class CommonController extends Controller
{
    protected $mustLogin = [];
    protected $except = [];
    protected $actions = ['*'];
    protected $verbs = [];
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => $this->actions,
                'except'=>$this->except,
                'rules' => [
                    [
                        'allow' => false,
                        'roles' => ['?'],
                        'actions' =>$this->mustLogin,
                    ],
                    [
                        'allow' => true,
                        'actions' => $this->mustLogin,
                        'roles' => ['@']
                    ]
                ]
            ],
            'verbs'=>[
                'class'=>VerbFilter::className(),
                'actions'=>$this->verbs
            ]
        ];
    }

    public function init()
    {
        $cate = Category::getMenu();
        $this->view->params['Menu'] = $cate;
        $data = [];
        $data['products'] = [];
        $total = 0;


        if (Yii::$app->session['isLogin']) {
            $usermodel = User::find()->where('username = :name', [":name" => Yii::$app->session['loginname']])->one();
            if (!empty($usermodel) && !empty($usermodel->userid)) {
                $userid = $usermodel->userid;
//                $carts = Cart::find()->where('userid = :uid', [':uid' => $userid])->asArray()->all();
//                foreach($carts as $k=>$pro) {
//                    $product = Product::find()->where('productid = :pid', [':pid' => $pro['productid']])->one();
//                    $data['products'][$k]['cover'] = $product->cover;
//                    $data['products'][$k]['title'] = $product->title;
//                    $data['products'][$k]['productnum'] = $pro['productnum'];
//                    $data['products'][$k]['price'] = $pro['price'];
//                    $data['products'][$k]['productid'] = $pro['productid'];
//                    $data['products'][$k]['   cartid'] = $pro['cartid'];
//                    $total += $data['products'][$k]['price'] * $data['products'][$k]['productnum'];
//                }
            }
        }
        $data['total'] = $total;
        $this->view->params['cart'] = $data;
        $tui = Product::find()->where('istui = "1" and ison = "1"')->orderby('createtime desc')->limit(3)->all();
        $new = Product::find()->where('ison = "1"')->orderby('createtime desc')->limit(3)->all();
        $hot = Product::find()->where('ison = "1" and ishot = "1"')->orderby('createtime desc')->limit(3)->all();
        $sale = Product::find()->where('ison = "1" and issale = "1"')->orderby('createtime desc')->limit(3)->all();
        $this->view->params['tui'] = (array)$tui;
        $this->view->params['new'] = (array)$new;
        $this->view->params['hot'] = (array)$hot;
        $this->view->params['sale'] = (array)$sale;
    }
}